<?php
namespace ac\controllers;

use Yii;
use yii\web\Controller;
use app\models\Opened;
use app\models\Company;
use common\component\exception\ErrMap;
use common\models\OperationLog;

class BasicController extends Controller
{
    protected $get;
    protected $post;
    protected $headers;
    protected $session;
    protected $params;
    
	/*所有控制器，必须执行的方法 优先于beforeAction前执行*/
	public function init()
    {
        $this->get = Yii::$app->request->get();
        $this->post = Yii::$app->request->post();
        $this->headers = Yii::$app->request->headers;
        $this->session = Yii::$app->session;
        $this->params = Yii::$app->params;
    }

    public function beforeAction($action)
    {
        $module = $this->module->id;
        $controller = $this->id;
        $action = $this->action->id;

        if($module == 'member'){
            if(isset($_SERVER['HTTP_ORIGIN'])){
                $origin = $_SERVER['HTTP_ORIGIN'];
                array_push(Yii::$app->params['allow_origin'], $origin);
            }else{
                $origin = '*';
            }
        }else{
            $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '*';
        }
        // P($origin);
        // sleep(1);
        // $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : 'http://t1.com';
        // $allow_origin = array(  
        //     'http://t1.com',
        //     'chrome-extension://pcppojmnbplflaohhidncmdnbkikphkd',
        //     'chrome-extension://aaanfbkieppipjomgpoifgegghdmfajh',
        //     'http://localhost:8080',
        //     'http://10.9.87.104:8080',
        //     'http://10.9.87.12:8080',
        //     'http://miniprogram.konglongyuan.com',
        //     'http://admin.raysfly.com'
        // );
        // P(Yii::$app->params['allow_origin']);
        // $origin = '*';
        if(in_array($origin, Yii::$app->params['allow_origin'])){
            header('Access-Control-Allow-Origin:'.$origin);
            header('Access-Control-Allow-Methods:POST,GET');
            header('Access-Control-Allow-Credentials:true');
            return true;
        }

        //非合法域
        list($errno, $errmsg) = ErrMap::getError(404);
        $args = array(
            'loginid' => 0,
            'loginname' => '',
            'back_code' => $errno,
            'back_info' => $errmsg,
        );
        if(gettype($res = OperationLog::addLog($args)) != 'boolean'){//操作日志
            Yii::$app->end($res);
        };
    }

    /*在openid表 根据appid 查询 comid*/
    public function getComidByAppid($appid)
    {
        $opened = new Opened;
        $opened = $opened::find()
                    ->select('comid')
                    ->where('appid = :appid', [':appid' => $appid])
                    ->asArray()->one();
        if(!is_null($opened)){
            return $opened['comid'];
        }else{
            return '';
        }
    }

    /*在company表 根据comid 查询 com_id*/
    public function getComIdByComid($comid)
    {
        $company = new Company;
        $company = $company::find()
                    ->select('com_id')
                    ->where('comid = :comid', [':comid' => $comid])
                    ->asArray()->one();
        // P($company);
        if(!is_null($company)){
            return $company['com_id'];
        }else{
            return '';
        }
    }

}
